import Vue from 'vue'
import Vuex from 'vuex'
import {get} from '../plugins/axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // user: ''
    // 不使用token 因此直接从本地获取
    user: sessionStorage.getItem('user')
  },
  mutations: {
    SAVE_USER(state, info) {
      state.user = info
    }
  },
  actions: {
     POST_USER({ commit }) {
      return new Promise(() => {
        get(`/post`).then(res => {
          console.log(typeof res[0].name);
          sessionStorage.setItem('user',res[0].name)
          commit('SAVE_USER', res[0].name)
        }).catch(err => {
          console.log(err);
        })
      })
    }

  },
  modules: {
  }
})
